package com.moonstar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;

public class ProcessAClient {
    //SendMessage的通信端口号
    public static final int SENDMESSAGE_PORT = 8880;

    //SendMessage的通信ip
    public static final String SENDMESSAGE_IP = "127.0.0.1";

    //内存映像文件名
    public static final String MEMORYFILE_NAME="MemoryFile.txt";

    //管道文件名
    public static final String PIPEFILE_NAME="PipeFile.txt";

    //判断此时进程b是否被创建
    public static boolean isCreateB = false;

    public static void main(String[] args) {
        //绘制进程A的界面
        JFrame jFrame = new JFrame("进程A");

        //流式布局，左对齐
        jFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
        jFrame.setBounds(500, 500, 260, 400);

        //JTextArea消息框
        JTextArea jTextArea = new JTextArea(10, 20);
        //自动换行
        jTextArea.setLineWrap(true);

        //jScrollPane用于装载text消息框
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        jScrollPane.setSize(250, 200);

        //构建sendMessageButton按钮
        JButton sendMessageButton = new JButton("利用SendMessage发送消息");
        sendMessageButton.setSize(200, 20);
        sendMessageButton.addActionListener(new AbstractAction() {
            //当SendMessageButton被点击的时候，向进程B的端口号发送消息
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text = jTextArea.getText();
                    DatagramSocket socket = new DatagramSocket();
                    socket.send(new DatagramPacket(text.getBytes(),text.getBytes().length, InetAddress.getByName(SENDMESSAGE_IP),SENDMESSAGE_PORT));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        //构建"写数据到内存映像文件"按钮
        JButton outToMemoryFileButton = new JButton("写数据到内存映像文件  ");
        outToMemoryFileButton.setSize(200, 20);
        outToMemoryFileButton.addActionListener(new AbstractAction() {
            //当outToMemoryFileButton被点击的时候，创建一个新文件并且存储text框中的信息.
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text = jTextArea.getText();
                    FileOutputStream fout = new FileOutputStream(new File(MEMORYFILE_NAME),false);
                    byte[] bytes = text.getBytes();
                    fout.write(bytes,0,bytes.length);
                    fout.close();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });

        //构建"写数据到管道文件"的按钮
        JButton outToPipeFileButton = new JButton("写数据到管道文件");
        outToPipeFileButton.setSize(200, 20);
        outToPipeFileButton.addActionListener(new AbstractAction() {
            //当outToPipeFileButton被点击的时候，向管道文件中写数据，但是写之前需要判断管道文件是否存在
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断对应文件是否存在
                File pipeFile = new File(PIPEFILE_NAME);
                if(!pipeFile.exists()){
                    //如果不存在直接返回
                    JOptionPane.showConfirmDialog (null, "不存在对应的管道文件"
                            , "友情提示", JOptionPane.CANCEL_OPTION, JOptionPane.OK_OPTION);
                    return;
                }
                try {
                    String text = jTextArea.getText();
                    FileOutputStream fout = new FileOutputStream(pipeFile,false);
                    byte[] bytes = text.getBytes();
                    fout.write(bytes,0,bytes.length);
                    fout.close();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });


        //构建创建进程B的按钮
        JButton createProcessB = new JButton("创建进程B");
        sendMessageButton.setSize(70, 20);
        createProcessB.addActionListener(new AbstractAction() {
            //点击进程B的时候判断是否进程B被创建
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!isCreateB) {
                    //此时B没有被创建则创建一个新的线程B
                    ProcessBThread processBThread = new ProcessBThread();
                    Thread thread = new Thread(processBThread);
                    thread.start();
                    //将标记置为true;
                    isCreateB = true;
                }
                sendMessageButton.setSize(200, 20);
                //否则就什么都不做
            }
        });

        //构建退出窗口按钮
        JButton exitButton = new JButton("退出");
        exitButton.setSize(70,20);
        exitButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File file = new File(MEMORYFILE_NAME);
                if(file.exists()){
                    file.delete();
                }
                System.exit(0);
            }
        });


        //jFrame装填各种组件
        jFrame.add(jScrollPane);
        jFrame.add(sendMessageButton);
        jFrame.add(outToMemoryFileButton);
        jFrame.add(outToPipeFileButton);
        jFrame.add(createProcessB);
        jFrame.add(exitButton);

//        jFrame.setResizable(false);    //TODO 最后调试代码结束可做限制
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}
